package fun.yyds.common.util;

import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.support.ExcelTypeEnum;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;

import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.net.URLEncoder;
import java.util.List;


@Slf4j
public class ExcelUtil {


    @SneakyThrows
    public static <T> void exportDataWithTpl(HttpServletResponse response, String excelName, List<T> dataList, String sheetName) {
        response.setCharacterEncoding("UTF-8");
        response.setHeader("Content-Type", "application/vnd.ms-excel");
        response.setHeader("Content-Disposition",
                "attachment;filename=" + URLEncoder.encode(excelName, "UTF-8"));
        // 导出：
        String templatePath = FileUtil.getPath() + "excelTpl" + File.separator + "系统能效损耗数据导出模板.xlsx";
        EasyExcel.write(response.getOutputStream())
                .excelType(ExcelTypeEnum.XLSX)
                .withTemplate(templatePath)
                .sheet(sheetName)
                .sheetNo(0)
                .doFill(dataList);

    }


}
